package com.xxzx.publicprevention.service.impl;


import com.xxzx.publicprevention.pojo.UserDO;
import com.xxzx.publicprevention.mapper.UserMapper;
import com.xxzx.publicprevention.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liyu
 * @since 2020-10-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public List<UserDO> getAllUsers() {
        return userMapper.getAllUsers();
    }

    @Override
    public UserDO getUser(Integer id) {
        UserDO user = userMapper.getUser(id);
        return user;

    }



    @Override
    public UserDO getUserByOpenIdAndwxName(String openId, String wxname) {
        UserDO user = userMapper.getUserByOpenId(openId);
        if (user == null) {
            user = new UserDO();
            user.setOpenId(openId);
            user.setStatus(0);
            user.setWxName(wxname);
            userMapper.addUser(user);
        }
        if (!user.getWxName().equals(wxname)) {
            user.setWxName(wxname);
            userMapper.update(user);
        }
        return user;
    }

    @Override
    public UserDO getUserByOpenId(String openId) {
        UserDO user = userMapper.getUserByOpenId(openId);
//        if (user == null) {
//            user = new UserDO();
//            user.setOpenId(openId);
//            user.setStatus(0);
//            userMapper.addUser(user);
//        }
        return user;

    }

    @Override
    public UserDO getUserByPhoneNumber(String phoneNumber) {
        return userMapper.getUserByPhoneNumber(phoneNumber);
    }

    @Override
    public List<UserDO> getAllUserInOpenIdNotNull() {
        return userMapper.getAllUserInOpenIdNotNull();
    }

    @Override
    public List<UserDO> getUserByLocationCity(String locationCity) {
        return userMapper.getUserByLocationCity(locationCity);
    }

    @Override
    public List<UserDO> getUserByLocationStreet(String locationStreet) {
        return userMapper.getUserByLocationStreet(locationStreet);
    }

    @Override
    public UserDO getUserInfo(String openId, String wxName, String unionId) {
        UserDO user = userMapper.getUserByOpenId(openId);
        if (user == null && !openId.equals("null") && !openId.equals("undefined")) {
            UserDO newUser = new UserDO();
            newUser.setOpenId(openId);
            newUser.setWxName(wxName);
            newUser.setUnionId(unionId);
            newUser.setStatus(0);
            userMapper.addUser(newUser);
            return getUserInfo(openId,wxName,unionId);
        }
        return user;
    }

    @Override
    public Boolean checkUserPhoneNumber(Integer userId) {
        UserDO user = userMapper.getUser(userId);
        try {
            if (user.getPhoneNumber() != null && user.getPhoneNumber().length() > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }

    @Override
    public Map<String, String> bindPhoneNumberAndRealName(Integer userId, String phoneNumber, String realName) {
        Map<String, String> res = new HashMap<>();
        List<UserDO> checkRes = userMapper.uniquenessPhoneVerification(phoneNumber);
        try {
            if (checkRes.size() > 0) {
                // 有重复的 返回status = "err", status_text = "数据库发现重复"；
                res.put("status", "err");
                res.put("status_text", "电话号码已被绑定，请联系管理员处理！");
            } else {
                // 没有重复的 update phoneNumber和realName，返回status = "success", status_text = "操作成功"；
                UserDO userDO = getUser(userId);
                userDO.setPhoneNumber(phoneNumber);
                userDO.setRealName(realName);
                userMapper.update(userDO);
                // todo 【未完成】获取平台用户表对比，获得角色

                res.put("status", "success");
                res.put("status_text", "操作成功");
            }
        } catch (Exception e) {
            System.out.println(e);
            res.put("status", "err");
            res.put("status_text", "后端错误：UserServiceImpl-bindPhoneNumberAndRealName");
        }
        return res;
    }

    @Override
    public Map<String, String> updateUserLocation(Integer userId, String locationCity, String locationStreet) {
        Map<String, String> res = new HashMap<>();
        try {
            UserDO userDO = getUser(userId);
            userDO.setLocationCity(locationCity);
            userDO.setLocationStreet(locationStreet);
            userMapper.update(userDO);
            res.put("status", "success");
            res.put("status_text", "操作成功");
        } catch (Exception e) {
            res.put("status", "err");
            res.put("status_text", "后端错误：UserServiceImpl-updateUserLocation");
        }
        return res;
    }

    @Override
    public int getCount() {
        return userMapper.getCount();
    }

    @Override
    public List<UserDO> getUserByPage(Integer curr, Integer size) {
        List<UserDO> userList = userMapper.getUserByPage(curr, size);
        return userList;    }

    @Override
    public void editUser(UserDO userDO) {
        userMapper.update(userDO);
    }

}
